//    Copyright (C) Kherty.  All rights reserved.
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Chart.Internals;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Chart
{
    public abstract class Series : ViewModelBase,IOfficeXmlSerializable, IShape
    {
        private ExtensionElement _extLst;
        private int _index;

        private SeriesText _tx;
        private int _order;
        private ShapeProperties _spPr;

        public ExtensionElement Extension
        {
            get { return _extLst; }
            set { _extLst = value; notifyPropertyChanged("Extension");}
        }

        public int Index
        {
            get { return _index; }
            set
            {
                _index = value;
                notifyPropertyChanged("Index");
            }
        }

        public int Order
        {
            get { return _order; }
            set
            {
                _order = value;
                notifyPropertyChanged("Order");
            }
        }

        public ShapeProperties ShapeProperties
        {
            get
            {
                if (_spPr == null)
                    _spPr = new ShapeProperties();
                return _spPr;
            }
            set
            {
                _spPr = value;
                notifyPropertyChanged("ShapeProperties");
            }
        }

        public SeriesText Text
        {
            get
            {
                if (_tx == null)
                    return new SeriesText(StringTable.Series + (Index + 1));
                return _tx;
            }
            set
            {
                _tx = value;
                notifyPropertyChanged("Text");
            }
        }


        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            ReadXmlCore(context);
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            WriteXmlCore(context);
        }

        #endregion

        internal void WriteOptionalNumericDataSource(string name, IOfficeXmlSerializable ds, WriteContext context)
        {
            if (ds == null)
                return;
            WriteNumericDataSource(name, ds, context);
        }

        internal static void WriteNumericDataSource(string name, IOfficeXmlSerializable ds, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                c.WriteElement(ds);
            }
        }

        protected void ReadXmlBase(ReadContext c)
        {
            //\\ThemeOwner = ReadContextData.GetThemeOwner(c);
            Index = c.ReadElementIntegerAttribute("idx", "val");
            Order = c.ReadElementIntegerAttribute("order", "val");
            Text = c.ReadOptionalElement<SeriesText>("tx");
            ShapeProperties = c.ReadOptionalElement<ShapeProperties>("spPr");
        }


        protected void WriteXmlBase(WriteContext c)
        {
            c.WriteElementIntegerAttribute("idx", "val", Index);
            c.WriteElementIntegerAttribute("order", "val", Order);
            c.WriteOptionalElement(Text);
            c.WriteOptionalElement(ShapeProperties);
        }

        internal abstract void ReadXmlCore(ReadContext context);

        internal abstract void WriteXmlCore(WriteContext context);

    }
}